package com.woniu.controller;


import com.woniu.entity.PageData;
import com.woniu.entity.Result;
import com.woniu.entity.dto.AdditionOrUpdateInspectorDTO;
import com.woniu.entity.dto.QueryInspectorDTO;
import com.woniu.entity.vo.*;
import com.woniu.pojo.Inspector;
import com.woniu.service.InspectorService;
import com.woniu.utils.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/inspector")
@Api("巡检员管理")
public class InspectorController extends BaseController {
    @Autowired
    private InspectorService inspectorService;

    @ApiOperation("分页+搜索")
    @PostMapping("/list/search/{currentPage}/{pageSize}")
    public Result listSearch(@PathVariable Integer currentPage,
                             @PathVariable Integer pageSize,
                             @RequestBody QueryInspectorDTO queryInspectorDTO){
        PageData pageData = inspectorService.listSearch(currentPage, pageSize, queryInspectorDTO);
        return toDataResult(pageData);
    }

    @ApiOperation("添加或修改巡检员")
    @PostMapping("/add")
    public Result addInspector(@RequestBody AdditionOrUpdateInspectorDTO additionOrUpdateInspectorDTO){
        if (!additionOrUpdateInspectorDTO.getInspectorName().isEmpty() &&
                !additionOrUpdateInspectorDTO.getInspectorPhone().isEmpty() &&
                additionOrUpdateInspectorDTO.getStatus() != null &&
                additionOrUpdateInspectorDTO.getRoadId().length > 0) {
            int i = inspectorService.saveOrUpdated(additionOrUpdateInspectorDTO);
            if (i == 1) {
                return success("修改成功");
            }
            if (i == 2) {
                return success("添加成功");
            }
        }
        return error("操作失败");
    }

    @ApiOperation("删除巡检员")
    @GetMapping("/delete/{id}")
    private Result delete(@PathVariable String id){
        return toResult(inspectorService.removeById(id));
    }


    @ApiOperation("巡检员禁用")
    @GetMapping("/disabledInspector/{id}/{status}")
    public Result disabledUser(@PathVariable String id,@PathVariable Integer status){
        if (status>1){
            return error("禁用失败");
        }
        int i = inspectorService.disabledInspector(id, status);
        if (i>0) {
            return success("操作成功");
        }else {
            return error("操作失败");
        }
    }

    @ApiOperation("Id查询巡检员")
    @GetMapping("/queryInspectorById/{id}")
    public Result QueryById(@PathVariable String id){
        QueryInspectorVO queryInspectorVO = inspectorService.listSearchById(id);
        return toDataResult(queryInspectorVO);
    }

    @ApiOperation("查询巡检员管理下的路段")
    @GetMapping("/QueryRoad/{id}")
    public Result QueryRoad(@PathVariable String id){
        List<QueryRoadVO> roads = inspectorService.selectRoadById(id);
        return toDataResult(roads);
    }

    @ApiOperation("根据巡检员ID查询订单")
    @GetMapping("/QueryOrderByInspectorId/{id}")
    public Result QueryOrderByInspectorId(@PathVariable String id){
        List<QueryOrderByInspectorIdVO> orderByInspectorIdVOList = inspectorService.QueryOrderByInspectorId(id);
        return toDataResult(orderByInspectorIdVOList);
    }

    @ApiOperation("根据巡检员ID查询名字，手机，密码")
    @GetMapping("/QueryInspectorById/{id}")
    public Result QueryInspectorById(@PathVariable String id){
        QueryAddAndUpdeteInspectorVO queryAddAndUpdeteInspectorVOS = inspectorService.listInspectorById(id);
        return toDataResult(queryAddAndUpdeteInspectorVOS);
    }

    @ApiOperation("根据巡检员ID统计详情信息")
    @GetMapping("/QueryInspectorDetailedStatisticsById/{id}")
    public Result QueryInspectorDetailedStatisticsById(@PathVariable String id){
        InspectorDetailedStatisticsVO inspectorDetailedStatisticsVO  = inspectorService.QueryInspectorDetailedStatisticsById(id);
        return toDataResult(inspectorDetailedStatisticsVO);
    }

}
